package com.example.demo.sys.service.impl;

import com.example.demo.core.utils.BeanConvertor;
import com.example.demo.sys.dao.MessageMapper;
import com.example.demo.sys.query.MessageQuery;
import com.example.demo.sys.entity.Message;
import com.example.demo.sys.service.MessageService;
import com.example.demo.sys.service.bo.MessageBO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Override
    public List<MessageBO> getUserMessages(Long userId) {
        MessageQuery query = new MessageQuery();
        query.setUserId(userId);
        List<Message> msgs = messageMapper.getUserMessages(query);
        return convert(msgs);
    }

    @Override
    public List<MessageBO> getHomeMessages(Long userId) {
        MessageQuery query = new MessageQuery();
        query.setUserId(userId);
        List<Message> msgs = messageMapper.getUserMessages(query);
        return convert(msgs);
    }

    private List<MessageBO> convert(List<Message> msgs){
        List<MessageBO> result = new ArrayList<>();
        for(int i=0;i<msgs.size();i++){
            Message item = msgs.get(i);
            MessageBO messageBO = BeanConvertor.transfer(item, new MessageBO());
            result.add(messageBO);
        }
        return result;
    }
}
